﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NsfcSurvey.Web.Helper;
using NsfcSurvey.Web.BzService;

namespace NsfcSurvey.Web
{
    public partial class Input : System.Web.UI.Page
    {
        DataService dataService = new DataService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UserHelper.CheckSession())
                {
                    ltrDeptName.Text = UserHelper.GetDepartmentName();
                }
                else
                {
                    Response.Redirect("~/Index.aspx");
                }
            }
        }

        protected void BtnSubmitSurvey_Click(object sender, EventArgs e)
        {
            if (!ValidateInput())
                return;


            //收集用户填写信息，如果有的问题没有填写，需要提示

            //数据格式为 A,B,C;用户填写的内容

            Answer answer = new Answer();
            answer.DepartmentCode = UserHelper.GetDepartmentCode();
            answer.DepartmentName = UserHelper.GetDepartmentName();
            answer.Username = UserHelper.GetUserName();
            answer.Phone = UserHelper.GetPhone();
            answer.Email = UserHelper.GetEmail();
            answer.Category = UserHelper.GetCategory1();
            answer.AnswerTime = DateTime.Now;

            answer.Answer1 = GetAnswer(cbl1);
            answer.Answer1Input = tbxUserInput1.Text.Trim();
            answer.Answer2 = GetAnswer(cbl2);
            answer.Answer2Input = tbxUserInput2.Text.Trim();
            answer.Answer3 = GetAnswer(cbl3);
            answer.Answer3Input = tbxUserInput3.Text.Trim();

            answer.Answer4 = GetAnswer(cbl4);
            answer.Answer5 = GetAnswer(cbl5);
            answer.Answer6 = GetAnswer(cbl6);
            answer.Answer7 = GetAnswer(cbl7);
            answer.Answer7Input = tbxUserInput7.Text.Trim();
            answer.Answer8 = GetAnswer(cbl8);

            answer.Answer9 = string.Format("{0},{1},{2}", tbxUserInput91.Text.Trim(), tbxUserInput92.Text.Trim(), tbxUserInput93.Text.Trim());
            answer.Answer10 = tbxUserInput10.Text.Trim();




            dataService.CreateAnswer(answer);

            //clear session
            UserHelper.ClearSession();

            Session["ExtUserError"] = "问卷调查结果已经提交成功！感谢您的参与！";

            Response.Redirect("~/Message.aspx");
        }

        private bool ValidateInput()
        {
            bool flag = true;
            StringBuilder sb = new StringBuilder("提示：<br/>");
            if (cbl1.SelectedItem == null)
            {
                sb.Append("第1题没有填写！<br/>");
                flag = false;
            }
            else if (GetSelected(cbl1, "F"))// choose F
            {
                if (string.IsNullOrEmpty(tbxUserInput1.Text))
                {
                    sb.Append("第1题必须填写其他！<br/>");
                    flag = false;
                }
            }
            if (cbl2.SelectedItem == null)
            {
                sb.Append("第2题没有填写！<br/>");
                flag = false;
            }
            else if (GetSelected(cbl2, "F"))// choose F
            {
                if (string.IsNullOrEmpty(tbxUserInput2.Text))
                {
                    sb.Append("第2题必须填写其他原因！<br/>");
                    flag = false;
                }
            }
            if (cbl3.SelectedItem == null)
            {
                sb.Append("第3题没有填写！<br/>");
                flag = false;
            }
            else if (GetSelected(cbl3, "F"))// choose F
            {
                if (string.IsNullOrEmpty(tbxUserInput3.Text))
                {
                    sb.Append("第3题必须填写其他规定！<br/>");
                    flag = false;
                }
            }
            if (cbl4.SelectedItem == null)
            {
                sb.Append("第4题没有填写！<br/>");
                flag = false;
            }
            if (cbl5.SelectedItem == null)
            {
                sb.Append("第5题没有填写！<br/>");
                flag = false;
            }
            if (cbl6.SelectedItem == null)
            {
                sb.Append("第6题没有填写！<br/>");
                flag = false;
            }
            if (cbl7.SelectedItem == null)
            {
                sb.Append("第7题没有填写！<br/>");
                flag = false;
            }
            else if (cbl7.SelectedValue.Equals("B"))// choose F
            {
                if (string.IsNullOrEmpty(tbxUserInput7.Text))
                {
                    sb.Append("第7题必须填写科目及比例！<br/>");
                    flag = false;
                }
            }
            if (cbl8.SelectedItem == null)
            {
                sb.Append("第8题没有填写！<br/>");
                flag = false;
            }
            if (string.IsNullOrEmpty(tbxUserInput91.Text.Trim()) || string.IsNullOrEmpty(tbxUserInput92.Text.Trim()) || string.IsNullOrEmpty(tbxUserInput93.Text.Trim()))
            {
                sb.Append("第9题没有填写！<br/>");
                flag = false;
            }

            if (!flag)
            {
                pnlErr.Visible = true;
                ltrErr.Text = sb.ToString();
            }

            else
            {
                pnlErr.Visible = false;
                ltrErr.Text = string.Empty;
            }

            return flag;
        }

        private string GetAnswer(CheckBoxList cbl)
        {
            string result = "";
            foreach (ListItem cbxItem in cbl.Items)
            {
                if (cbxItem.Selected)
                {
                    result += cbxItem.Value + ",";
                }
            }

            return result.TrimEnd(',');
        }

        private bool GetSelected(CheckBoxList cbl, string value)
        {
            foreach (ListItem cbxItem in cbl.Items)
            {
                if (cbxItem.Selected)
                {
                    if (cbxItem.Value.Equals(value))
                        return true;
                }
            }

            return false;
        }


        private string GetAnswer(RadioButtonList rbl)
        {
            return rbl.SelectedValue;
        }
    }
}